import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_study/chewie/chewie_play.dart';
import 'package:flutter_study/model/recommend_model.dart';
import 'package:flutter_study/util/navigator_manager.dart';
import 'package:video_player/video_player.dart';

class RecommendVideoPlayPage extends StatefulWidget {
  final RecommendItem item;

  const RecommendVideoPlayPage({Key key, @required this.item})
      : super(key: key);

  @override
  _RecommendVideoPlayPageState createState() => _RecommendVideoPlayPageState();
}

class _RecommendVideoPlayPageState extends State<RecommendVideoPlayPage>
    with WidgetsBindingObserver {
  VideoPlayerController _videoPlayerController;
  ChewieController _chewieController;

  @override
  void initState() {
    super.initState();
    _videoPlayerController =
        VideoPlayerController.network(widget.item.data.content.data.playUrl);
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.paused) {
      _chewieController.pause();
    }
  }

  @override
  void dispose() {
    _videoPlayerController.dispose();
    _chewieController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion(
        child: Scaffold(
          body: Container(
            decoration: BoxDecoration(color: Colors.black),
            child: Stack(
              children: [
                Align(
                  child: Chewie(
                    controller: _getCheWieController(),
                    hideBackArrow: true,
                  ),
                ),
                Positioned(child: GestureDetector(
                  onTap: () {
                    NavigatorManager.back();
                  },
                  child: Container(
                    decoration: BoxDecoration(
                        color: Colors.grey,
                        borderRadius: BorderRadius.circular(12)
                    ),
                    width: 24,
                    height: 24,
                    child: Icon(Icons.keyboard_arrow_down, size: 20),
                  ),
                ), left: 10, top: MediaQuery
                    .of(context)
                    .padding
                    .top + 10)
              ],
            ),
          ),
        ),
        value: SystemUiOverlayStyle.light);
  }

  _getCheWieController() {
    double aspectRatio;
    bool allowFullScreen = widget.item.data.content.data.height >
        widget.item.data.content.data.width;
    if (allowFullScreen) {
      final size = MediaQuery
          .of(context)
          .size;
      var width = size.width;
      var height = size.height;
      aspectRatio = width / height;
    }
    _chewieController = ChewieController(
        videoPlayerController: _videoPlayerController,
        aspectRatio: aspectRatio,
        autoPlay: true,
        looping: true,
        allowFullScreen: !allowFullScreen
    );
    return _chewieController;
  }
}
